package com.lazy.app.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.transition.MaterialSharedAxis;
import com.lazy.app.R;
import com.lazy.app.databinding.FragmentRegisterBinding;
import com.lazy.app.presenter.RegisterPresenter;
import com.zndroid.base.ui.common.CommonFragment;
import com.zndroid.widget.title.TitleBar;

public class RegisterFragment extends CommonFragment<FragmentRegisterBinding, RegisterPresenter> {
    public static final int TYPE_REG = R.layout.layout_1;
    public static final int TYPE_HEAD = R.layout.layout_2;
    public static final int TYPE_GENDER = R.layout.layout_3;
    public static final int TYPE_INDIVIDUATION = R.layout.layout_4;

    private int[] RES_ID = new int[] {TYPE_REG, TYPE_HEAD, TYPE_GENDER, TYPE_INDIVIDUATION};

    private final Bundle bundle = new Bundle();
    private int index;

    @Override
    protected FragmentRegisterBinding onSupplyViewBinding(@NonNull LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup) {
        return FragmentRegisterBinding.inflate(layoutInflater, viewGroup, false);
    }

    @Override
    protected TitleBar onSupplyTitleBar() {
        return currentViewBinding().top.tbTitle;
    }

    @Override
    protected RegisterPresenter onSupplyPresenter() {
        return new RegisterPresenter();
    }

    @Override
    protected void onBackClickTitleRight() {
        if (index == RES_ID.length - 1) {
            requireActivity().finish();
        } else {
            next();
        }
    }

    @Override
    protected void doOnCreate(@Nullable Bundle savedInstanceState) {
        super.doOnCreate(savedInstanceState);

        replaceFragment(RES_ID[0]);
        updateUi(index);
    }

    @Override
    protected void doOnInitViewAfterAllReady(View rootView) {
        super.doOnInitViewAfterAllReady(rootView);

        currentViewBinding().bottom.preBtn.setOnClickListener(v -> pre());
        currentViewBinding().bottom.nextBtn.setOnClickListener(v -> next());
    }

    private void pre() {
        if (index != 0) {
            replaceFragment(RES_ID[--index]);
            updateUi(index);
        }
    }

    private void next() {
        if (index != RES_ID.length - 1) {
            replaceFragment(RES_ID[++index]);
            updateUi(index);
        } else {
            requireActivity().finish();
        }
    }

    private void updateUi(int index) {
        currentViewBinding().bottom.preBtn.setVisibility(index == 0 ? View.GONE : View.VISIBLE);
        currentViewBinding().bottom.nextBtn.setVisibility(index == (RES_ID.length-1) ? View.GONE : View.VISIBLE);
        currentViewBinding().bottom.nextBtn.setText("下一步");

        switch (index) {
            case 0:
                currentViewBinding().top.tbTitle.setTitle("手机号注册");
                currentViewBinding().top.tbTitle.getRightView().setVisibility(View.GONE);
                currentViewBinding().top.tbTitle.getLeftView().setVisibility(View.VISIBLE);
                break;
            case 1:
                currentViewBinding().top.tbTitle.setTitle("上传头像");
                currentViewBinding().top.tbTitle.getRightView().setVisibility(View.VISIBLE);
                currentViewBinding().top.tbTitle.getLeftView().setVisibility(View.GONE);
                break;
            case 2:
                currentViewBinding().top.tbTitle.setTitle("设置性别");
                currentViewBinding().top.tbTitle.getRightView().setVisibility(View.VISIBLE);
                currentViewBinding().top.tbTitle.getLeftView().setVisibility(View.GONE);
                break;
            case 3:
                currentViewBinding().top.tbTitle.setTitle("个性化定制");
                currentViewBinding().top.tbTitle.getRightView().setVisibility(View.VISIBLE);
                currentViewBinding().top.tbTitle.getLeftView().setVisibility(View.GONE);

                currentViewBinding().bottom.nextBtn.setVisibility(View.VISIBLE);
                currentViewBinding().bottom.nextBtn.setText("完成");
                break;
            default:
                currentViewBinding().top.tbTitle.getRightView().setVisibility(View.GONE);
                currentViewBinding().top.tbTitle.getLeftView().setVisibility(View.VISIBLE);
                break;
        }
    }

    private void replaceFragment(@LayoutRes int layoutResId) {
        RegisterGuideFragment fragment = RegisterGuideFragment.newInstance(layoutResId, bundle);
        // Set the transition as the Fragment's enter transition. This will be used when the fragment
        // is added to the container and re-used when the fragment is removed from the container.
        fragment.setEnterTransition(createTransition(layoutResId != RES_ID[0]));

        getChildFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, fragment)
                .commit();
    }

    private MaterialSharedAxis createTransition(boolean entering) {
        MaterialSharedAxis transition =
                new MaterialSharedAxis(MaterialSharedAxis.Z, entering);

        // Add targets for this transition to explicitly run transitions only on these views. Without
        // targeting, a MaterialSharedAxis transition would be run for every view in the
        // Fragment's layout.
        // transition.addTarget(...);

        return transition;
    }
}
